﻿#include "examinterface.h"
#include <QFont>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QCheckBox>
#include <QPushButton>
#include <QMessageBox>
#include <QApplication>

typedef enum class STATU
{
    BEGIN,
    QUESTION,
    ITEMS,
    ANSWER
}STATU;

examInterface::examInterface(QWidget*parent):QDialog(parent),line_per_num(10)
{
    QFont font;
    font.setPointSize(12);
    setFont(font);
    setPalette(QColor(123,241,56));
    resize(1100,1200);

    setWindowTitle("考试已用时: 0分0秒");
    setWindowFlags(Qt::Dialog|Qt::WindowCloseButtonHint|Qt::WindowMinMaxButtonsHint);
    initTimer();
    initLayout();
    if(!initTextEdit())
    {
        QTimer::singleShot(0,qApp,SLOT(quit()));
        return;
    }
    initButtons();
    show();
}

examInterface::~examInterface()
{
    timer->stop();
    delete timer;
}

void examInterface::initTimer()
{
    timeGo=0;
    timer=new QTimer(this);
    timer->setInterval(1000);
    timer->start();
    connect(timer,SIGNAL(timeout()),this,SLOT(updateTime()));
}

void examInterface::initLayout()
{
    layout=new QGridLayout(this);
    layout->setSpacing(10);
    layout->setMargin(10);
}

int examInterface::Qlines=0;
bool examInterface::initTextEdit()
{
    textEdit=new QTextEdit(this);


    QString filename("../exam.txt");
    QFile file(filename);
    QTextStream stream(&file);
    stream.setCodec("UTF-8");

    STATU statu=STATU::BEGIN;
    bool needRead=true;

    QString qlabel;

    QStringList item;
    QStringList answers;


    if(file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        QString strLine;
        QString Text;

        while(1)
        {


            if(needRead)
            {
                if(stream.atEnd())break;
                strLine=stream.readLine();

                strLine=strLine.trimmed();
                if(strLine.length()<1)continue;

                if(strLine.at(0)=="#")continue;
                qDebug()<<"读到行:"<<strLine<<endl;
            }
            else
            {
                needRead=true;
            }




            switch (statu)
            {
            case STATU::BEGIN:
                answers.clear();
                item.clear();
                if(strLine.at(0)=="Q")
                {
                    if(Qlines!=0)
                    {
                        Text.append('\n');
                    }
                    Qlines++;
                    Text.append(strLine.remove(0,1));
                    Text.append('\n');

                    needRead=false;
                    statu=STATU::QUESTION;
                }


                            break;
            case STATU::QUESTION:
            {

                    int pos=strLine.indexOf(".");

                    strLine.resize(pos);
                    qlabel=strLine;
                    qDebug()<<"获取到题目标签:"<<qlabel<<endl;

                    statu=STATU::ITEMS;

                            break;
            }
            case STATU::ITEMS:
                    if(strLine.at(0)==".")
                    {
                       Text.append(strLine.remove(0,1));
                       Text.append('\n');
                       int pos=strLine.indexOf(" ");
                       if(pos>0)
                       {
                           strLine.resize(pos);
                           item.append(strLine);
                           qDebug()<<"获取到选项:"<<item<<endl;
                       }
                       else
                       {
                           item.append(strLine);
                           qDebug()<<"获取到选项:"<<item<<endl;
                       }

                    }
                    else if(strLine.at(0)=="!")
                    {
                        needRead=false;
                        statu=STATU::ANSWER;
                    }
                    else
                    {
                        statu=STATU::ANSWER;
                    }
                            break;
            case STATU::ANSWER:
                if(strLine.at(0)=='!')
                {
                    strLine.remove(0,1);
                    if(strLine[0]>'a'&&strLine[0]<'z'||strLine[0]>'A'&&strLine[0]<'Z')
                    {
                        for(int i=0;i<strLine.size();i++)
                        {
                            answers.append(QString(strLine[i]));
                            qDebug()<<"获取到答案:"<<answers[i]<<endl;
                        }
                    }
                    else
                    {
                        answers.append(strLine);
                    }
                    //answers.append(strLine);


                    answeritem*ai=new answeritem(qlabel,answers);
                    for(int i=0;i<item.size();i++)
                    {
                        ai->setItems(item[i]);
                    }
                    items.append(ai);


                    statu=STATU::BEGIN;
                }
                else
                {
                    QMessageBox::information(this,"错误","答案为空,请设置好答案,题目"+qlabel);
                    return false;
                }

                            break;
            }


//            if(strLine.at(0)=="!")continue;

//            if(strLine.at(0)==".")
//            {
//                Text.append(strLine.remove(0,1));
//            }

        }

        textEdit->setText(Text);
        layout->addWidget(textEdit,0,0,1,10);
        file.close();
        return true;
    }
    else
    {
        return false;
    }


}

void examInterface::initButtons()
{
//    for(int i=0;i<Qlines;i++)
//    {
//        QLabel*label=new QLabel("第"+QString::number(i+1)+"题");
//        labels.append(label);
//    }
//    for(int i=0;i<labels.count();i++)
//    {
//        layout->addWidget(labels[i],1,i);
//    }
    int lineNO=1,maxItemNum=0;
    qDebug()<<items.size();
   for(int i=0;i<items.size();i++)
   {
       if(i%line_per_num==0&&i!=0)
       {
           lineNO+=maxItemNum+1;
           maxItemNum=0;
       }
       QLabel*label=new QLabel("第"+items[i]->getLabel()+"题");
       layout->addWidget(label,lineNO,i%line_per_num);

       if(items[i]->getNum()>maxItemNum)
       {
           maxItemNum=items[i]->getNum();
       }
       qDebug()<<items[i]->getItems();
       QVector<QPair<QString,int>> &item=items[i]->getItems();
       if(items[i]->getIsSigned())
       {
           QButtonGroup *bg=new QButtonGroup;
           for(int j=0;j<items[i]->getNum();j++)
           {
               QRadioButton*rb=new QRadioButton;
               rb->setText(item[j].first);
               bg->addButton(rb);
               layout->addWidget(rb,lineNO+j+1,i%line_per_num);
           }
           buttonGroup.append(bg);
       }
       else
       {

           QButtonGroup *cbg=new QButtonGroup;
           cbg->setExclusive(false);
           for(int j=0;j<items[i]->getNum();j++)
           {
               QCheckBox*cb=new QCheckBox;
               cb->setText(item[j].first);
               cbg->addButton(cb);
               layout->addWidget(cb,lineNO+j+1,i%line_per_num);
           }
           buttonGroup.append(cbg);
       }

   }



   QPushButton *pushBtn=new QPushButton;
   pushBtn->setText("提交");
   pushBtn->setFixedSize(120,80);
   layout->addWidget(pushBtn,lineNO+maxItemNum+1,line_per_num-1);
   connect(pushBtn,SIGNAL(clicked()),this,SLOT(getScore()));



}

void examInterface::updateTime()
{
    timeGo++;
    QString timeStr;
    timeStr="考试已用时: "+QString::number(timeGo/60)+"分"+QString::number(timeGo%60)+"秒";
    setWindowTitle(timeStr);
}

void examInterface::getScore()
{
    //QMessageBox::information(this,"提示","hello");

    int Score=10;
    int totalScore=0;
    for(int i=0;i<items.size();i++)
    {
        if(!buttonGroup[i]->checkedButton())
        {
            QMessageBox::information(this,"提示","您还有选项没有选择!");
            return;
        }

        if(items[i]->getIsSigned())
        {
            if(buttonGroup[i]->checkedButton()->text()==items[i]->getAnswers()[0])
            {
                totalScore+=Score;
            }
        }
        else
        {
            int selects=0;
            int valid=0;
            QList<QAbstractButton*>buttons= buttonGroup[i]->buttons();
            int j=0;
            QStringList list=items[i]->getAnswers();
            for(int i=0;i<buttons.size();i++)
            {
                if(buttons[i]->isChecked())
                {
                    selects++;
                    for(j=0;j<list.size();j++)
                    {
                        if(list[j]==buttons[i]->text())
                        {
                            break;
                        }
                    }
                    if(j==list.size())
                    {
                        break;
                    }
                    valid++;
                }
            }

            if(selects==valid&&valid==list.size())
            {
                totalScore+=Score;
            }

        }
    }

    int res =QMessageBox::information(this,"提示","您的考试分数为:"+QString::number(totalScore)+"分,是否重考",QMessageBox::Yes|QMessageBox::No);
    if(res==QMessageBox::Yes)
    {
        return;
    }
    else
    {
        close();
    }
}
